/**
 * @(#) RestaurantController.java
 */

package TheRestaurantOwner;

import java.util.Random;

public class RestaurantController
{
	private MenuController menuController;
	private Restaurant restaurant;
	private Client[] population;
	private RankingList rankingList;

	private int day;
	
	
	public RestaurantController( )
	{
		rankingList = new RankingList();
		rankingList.addToRankingList("David", 800);
		rankingList.addToRankingList("James", 1500);
		rankingList.addToRankingList("_xXCoDSnIp3rXx_", 1800);
	}
	
	public static void main( String[] args )
	{
		RestaurantController restaurantController = new RestaurantController();
		
		// Select restaurant name and address randomly
		String[] names = new String[]{"DinerSaur", "Metro", "Pancakes and Waffles", "McDonnell's", "Le Fancie Poisson"};
		String[] addresses = new String[]{"Tartu, Lai 58", "Tallinn, Vanemuise 19", "Oslo, Bjornstrasse 17", "Berlin, Wasserstrasse 119"};
		Random random = new Random();
		String restaurantName = names[random.nextInt(names.length)];
		String restaurantAddress = addresses[random.nextInt(addresses.length)];
		restaurantController.createRestaurant(restaurantName, restaurantAddress);

		System.out.println("You can choose the default value in brackets [] by pressing enter");
		
		Player player = new Player();
		restaurantController.startGame(player);
	}
	
	public void startGame( Player player )
	{
		// When a game starts, the player becomes owner of a restaurant.
		restaurant.setOwner(player);
		
		menuController.createMenu();
		
		// Clients are chosen from a population of 18 people.
		population = new Client[18];
		population[0] = new Client("Michael", "Pope", "555-1111", "TAX2");
		population[1] = new Client("Virginia", "Perry", "555-1112", "TAX2");
		population[2] = new Client("Louise", "Richardson", "555-1113", "TAX3");
		population[3] = new Client("Brenda", "Gonzalez", "555-1114", "TAX3");
		population[4] = new Client("Bonnie", "Washington", "555-1115", "TAX4");
		population[5] = new Client("Annie", "Flores", "555-1116", "TAX2");
		population[6] = new Client("Gary", "Allen", "555-1117", "TAX2");
		population[7] = new Client("Matthew", "Robinson", "555-1118", "TAX5");
		population[8] = new Client("Michelle", "Butler", "555-1119", "TAX5");
		population[9] = new Client("Dennis", "Rivera", "555-1121", "TAX2");
		population[10] = new Client("Timothy", "Roberts", "555-1131", "TAX2");
		population[11] = new Client("Harry", "Hall", "555-1141", "TAX2");
		population[12] = new Client("Shirley", "Harris", "555-1151", "TAX3");
		population[13] = new Client("Christopher", "Green", "555-1161", "TAX4");
		population[14] = new Client("Martha", "Torres", "555-1171", "TAX2");
		population[15] = new Client("Jesse", "Bailey", "555-1181", "TAX7");
		population[16] = new Client("Joan", "Wood", "555-1191", "TAX3");
		population[17] = new Client("Walter", "Collins", "555-1221", "TAX2");
		
		
		while (day < 30 && restaurant.getBudget() >= 0){
			startDay();
		}
		
		restaurant.payAdditionalCosts();
		
		if (restaurant.getBudget() >= 0){
			// Can get to the ranking list.
			System.out.println("Game over! Final score: " + restaurant.getBudget());
			rankingList.addToRankingList(restaurant.getOwner().getName(), restaurant.getBudget());
		} else {
			System.out.println("Game over! You ran out of money");
		}
		
		player.input("Press enter to see ranking list");
		rankingList.print();
		player.input("Press enter to see statistics");
		calculateStatisticsForAllClients();
	}

	public void createRestaurant( String name, String address )
	{
		restaurant = new Restaurant(name, address);
		System.out.println(
				"New restaurant \"" + name + "\"" +
				" at " + address.substring(address.indexOf(", ") + 2) +
				" now open in " + restaurant.getCity().getName() + "!");
		
		menuController = new MenuController(restaurant);
	}

	public void startDay( )
	{
		if (day != 0){
			// Process end of the previous day

			// The salaries are taken from the budget at the end of every week.
			if ((day % 7) == 0){
				System.out.print("End of the week, ");
				System.out.println("budget: " + restaurant.getBudget());
				restaurant.paySalaries();
				if (restaurant.getBudget() < 0){
					return;
				}
				System.out.println("Budget: " + restaurant.getBudget());
				restaurant.paySuppliers();
				if (restaurant.getBudget() < 0){
					return;
				}
			}
		}
		
		day++;
		System.out.println("Day " + day +
				", budget=" + restaurant.getBudget() +
				", reputation=" + restaurant.computeReputation()
				);
		
		restaurant.assignTablesToWaiters();
		
		// Choose clients for the day.
		// At a table there are always seated exactly 2 persons.
		int numClients = restaurant.getNumberOfOccupiedTables() * 2;
		Random random = new Random();
		java.util.ArrayList<Client> clients = new java.util.ArrayList<Client>();
		for (int i = 0; i < numClients; i++){
			Client client = population[random.nextInt(population.length)];
			while (clients.contains(client)){
				client = population[random.nextInt(population.length)];
			}
			clients.add(client);
		}
		Client[] clientsArray = new Client[numClients];
		clientsArray = clients.toArray(clientsArray);
		restaurant.receiveOrders(clientsArray);
		
		while (true){
			System.out.println("Commands: train - train an employee, day - start new day");
			String command = restaurant.getOwner().input("> ", "day");
			if (command.equals("day")){
				break;
			} else if (command.equals("train")){
				System.out.println("Which employee to train? (1-5)");
				Employee[] employees = restaurant.getEmployees();
				for (int i = 0; i < 5; i++){
					Employee employee = employees[i];
					System.out.println(new Integer(i+1).toString() + " " + employee.getClass().getSimpleName() + " " +
							employee.getName() + " " + employee.getSurname() + " Experience: " + employee.getExperience());
				}
				String employeeStr = restaurant.getOwner().input("> ", "1");
				int employeeNr = Integer.parseInt(employeeStr);
				Employee selectedEmployee = employees[employeeNr - 1];
				if (selectedEmployee.getTrainingCost() <= restaurant.getBudget()){
					restaurant.makePayment(selectedEmployee.sendToTraining());
					System.out.println("Training complete, experience: " + selectedEmployee.getExperience());
				} else {
					System.out.println("Not enough money for training!");
				}
			}
		}
	}

	public void calculateStatisticsForAllClients( )
	{
		for (Client client : population){
			client.calculateStatistics(restaurant.getMenu());
		}
		System.out.println("Total money spent " + restaurant.getMoneySpent());
	}
}
